package com.dg.lecheng.manager.controller;

import com.dg.lecheng.api.user.model.InfoRole;
import com.dg.lecheng.api.user.service.InfoRoleService;
import com.dg.lecheng.base.core.model.BaseResult;
import com.dg.lecheng.base.core.web.BaseController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Created by Administrator on 2017/10/22 0022.
 */
@Api(value = "InfoRoleController" ,description = "角色Controller")
@Controller
@RequestMapping("/infoRole")
public class InfoRoleController extends BaseController{

    @Autowired
    private InfoRoleService infoRoleService;

    @ApiOperation("获取角色列表")
    @RequestMapping(value = "/getList" ,method = RequestMethod.GET)
    @ResponseBody
    public Object getList(@ApiParam(name = "name" ,value = "角色名称") @RequestParam(value = "name",required = false) String name){
        List<InfoRole> infoRoles = infoRoleService.findByName(name,getPageBounds());
        return BaseResult.success(infoRoles);

    }

    @ApiOperation("获取单个角色")
    @RequestMapping(value = "get/{id}" ,method = RequestMethod.GET)
    @ResponseBody
    public Object get(@PathVariable int id){
        return BaseResult.success(infoRoleService.get(id));
    }

    /**
     * 添加角色信息
     * @param infoRole
     * @return
     */
    @ApiOperation("添加角色")
    @RequestMapping(value = "/add" ,method = RequestMethod.POST)
    @ResponseBody
    public Object add(@ApiParam(name = "infoRole" ,value = "用户model") @RequestBody InfoRole infoRole){
        if(infoRole.getId() == null){
            infoRoleService.add(infoRole);
        }else{
            infoRoleService.update(infoRole);
        }
        return BaseResult.success(null);
    }

    @ApiOperation("删除角色")
    @RequestMapping(value = "/delete/{id}" ,method = RequestMethod.POST)
    @ResponseBody
    public Object delete(@PathVariable int id){
        infoRoleService.removeRoleAndRelatedInfo(id);
        return BaseResult.success(null);
    }
}
